package com.caisejingyu.whales.common.utils;

import com.caisejingyu.whales.common.utils.singn.RequesUtil;
import com.caisejingyu.whales.common.utils.singn.XXTea;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignUtil {
    public static String encryptMD5(String val) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            md5.update(val.getBytes());
            byte[] bytes = md5.digest();//加密
            StringBuilder hexValue = new StringBuilder();
            for (byte m : bytes) {
                int tmp = ((int) m) & 0xff;
                if (tmp < 16)
                    hexValue.append("0");
                hexValue.append(Integer.toHexString(tmp));
            }
            return hexValue.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
    public static String getSign(String baseUrl) {
        String sign = "";
        String param = "{\"dcode\":\"12291\"}";
        String dataKey = "EF801B2547BA0E3F";
        String sessionkey = "E9F48B0E1AB3205A4B76B6AD07EE30F3";
        byte[] xxtea = new byte[0];
        byte[] pdata = param.getBytes();
        try {
            xxtea = XXTea.encrypt(pdata, dataKey.getBytes("utf-8"));
            //todo 签名
            sign = RequesUtil.getSign(baseUrl, xxtea, sessionkey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sign;
    }
    
}
